home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / clonecd / September 93.img / Archives / Decoration / Screensavers / Screen Savers / Darkness / dark.c < prev    next >
C/C++ Source or Header  |  1990-04-13  |  4KB  |  185 lines

  1. /*
  2.     DARK - a simple screen saver application for the 
  3.     "Darkness" Multifinder Screen Saver
  4.         written by Lunarmobiscuit  Copyright 1989
  5. */
  6.  
  7. /* EVENT */
  8. int done = FALSE;            /* do event loop until done == TRUE */
  9. int background = FALSE;        /* flag for background or foreground */
  10.  
  11. /* WINDOW */
  12. WindowPtr bigWindow;        /* the window */
  13.  
  14. /* MENU */
  15. MenuHandle appleMenu;        /* Multifinder demands one menu */
  16.  
  17. /* OS DEFINES */
  18. #define osEvt app4Evt
  19. #define suspend 1
  20. #define mouseMoved 0xFA
  21. #define resumeMask 1
  22.  
  23. /* PROTOTYPES */
  24. void EventLoop(void);
  25. void do_blackout(long time);
  26. int any_events(void);
  27. void update_big(void);
  28. void DoSuspend(EventRecord *theEvent);
  29. void DoUpdate(EventRecord *theEvent);
  30. void main(void);
  31. extern void InitMacintosh(void);
  32. extern void SetUpMenus(void);
  33. extern int SetUpBigWindow(void);
  34.  
  35.  
  36.  
  37. /* main event loop */
  38. void EventLoop(void)
  39. {
  40.     int                ok,any;        /* flags used to check events */
  41.     EventRecord        theEvent;    /* the event record for Dark */
  42.     Point            loc,old;    /* current and last mouse position */
  43.  
  44.     GetMouse(&old);  /* initialize mouse position */
  45.     for(;!done;) {
  46.         if (!background) {
  47.             any = any_events();  /* are there any events in the queue? */
  48.             GetMouse(&loc);  /* get the current mouse position */
  49.             /* check for cursor in the upper right corner (sleep now) */
  50.             if (loc.h > screenBits.bounds.right-3 && loc.h < screenBits.bounds.right+1 && loc.v > screenBits.bounds.top-1 && loc.v < screenBits.bounds.top+3)
  51.                 done = FALSE;
  52.             /* did the mouse move?  (5 pixels is more than bumping into the desk */
  53.             else if (old.h - loc.h > 3 || old.v - loc.v > 3 || loc.h - old.h > 3 || loc.v - old.v > 3)
  54.                 done = TRUE;
  55.             /* are there any events? */
  56.             else if (any)
  57.                 done = TRUE;
  58.             old = loc;  /* save the current mouse location */
  59.             
  60.             /****                                              ***/
  61.             /* INSERT YOUR CODE IN THE PROCEDURE do_blackout()     */
  62.             /****                                              ***/
  63.             do_blackout(TickCount());
  64.         }
  65.         
  66.         /* check the events to Dark */
  67.         /* 30L means Dark gets a null event at least every 30 ticks (i.e. 1/2 sec) */
  68.         ok = WaitNextEvent(everyEvent, &theEvent, 30L, 0L);
  69.         if (ok)
  70.           switch (theEvent.what)
  71.             {
  72.             case mouseDown:
  73.             case keyDown:
  74.             case autoKey:
  75.                 done = TRUE;
  76.                 break;
  77.             case updateEvt:
  78.                 DoUpdate(&theEvent);
  79.                 break;
  80.             case activateEvt:
  81.                 break;
  82.             case osEvt:
  83.                 DoSuspend(&theEvent);
  84.                 break;
  85.             }
  86.     }
  87.     return;
  88. }
  89.  
  90.  
  91. /* the drawing routine goes in here */
  92. void do_blackout(time)
  93. long time; /* you can change the parameter to be whatever you wish */
  94. {
  95.     /*
  96.         YOUR CODE GOES IN HERE
  97.     */
  98.     return;
  99. }
  100.  
  101.  
  102. /* look through the event queue (low-mem global) */
  103. int any_events(void)
  104. {
  105.     int            more = TRUE, any = FALSE;
  106.     EvQElPtr    evt;
  107.  
  108.     evt = (EvQElPtr)(EventQueue.qHead);
  109.     
  110.     if (!evt) return(FALSE);
  111.     while (more) {
  112.         if (!evt || evt == (EvQElPtr)(EventQueue.qTail)) more = FALSE;
  113.         if (evt->evtQWhat != nullEvent) {
  114.             any = TRUE;
  115.             more = FALSE;
  116.         }
  117.     }
  118.     return(any);
  119. }
  120.  
  121.  
  122. /* draw the contents of the big window */
  123. void update_big(void)
  124. {
  125.     GrafPtr oldPort;
  126.     
  127.     GetPort(&oldPort);
  128.     SetPort(bigWindow);
  129.     PenNormal();
  130.     PaintRect(&bigWindow->portRect);
  131.     SetPort(oldPort);
  132.     return;
  133. }
  134.  
  135.  
  136. /* multifinder (os) event */
  137. void DoSuspend(theEvent)
  138. EventRecord *theEvent;
  139. {
  140.     switch ((theEvent->message >> 24) & 0xFF) {
  141.         case mouseMoved:
  142.             break;
  143.         case suspend:
  144.             background = (theEvent->message & resumeMask) ? 0 : 1;
  145.             /* Dark quits before of becoming a background program */
  146.             if (background) done = TRUE;
  147.             break;
  148.     }
  149.     return;
  150. }
  151.  
  152.  
  153. /* big window needs updating */
  154. void DoUpdate(theEvent)
  155. EventRecord *theEvent;
  156. {
  157.     WindowPtr theWindow;
  158.     
  159.     theWindow = (WindowPtr) theEvent->message;
  160.     BeginUpdate(theWindow);
  161.         update_big();
  162.     EndUpdate(theWindow);
  163.     return;
  164. }
  165.  
  166.  
  167.  
  168. /* main code section */
  169. void main(void)
  170. {
  171.     int height = MBarHeight;  /* remember menu bar height */
  172.     
  173.     InitMacintosh();  /* initialize mac managers */
  174.     SetUpMenus();  /* must create a menu bar */
  175.     /* create the window and loop until event */
  176.     HideCursor();
  177.     if (SetUpBigWindow()) {
  178.         EventLoop();
  179.         MBarHeight = height;
  180.     }
  181.     ShowCursor();
  182.     ExitToShell(); /* th..th..that's all folks... */
  183. }
  184.  
  185.